package server;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;

/**
 * @Auther: Xing Fang Bin
 * @Date: 2021/08/12/16:50
 * @Description:
 */
public class RequestProcessor extends Thread {

    private Socket accept;
    private Map<String,HttpServlet> servletMap;

    public RequestProcessor(Socket accept, Map<String, HttpServlet> servletMap) {
        this.accept = accept;
        this.servletMap = servletMap;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = accept.getInputStream();
            Request request = new Request(inputStream);
            Response response = new Response(accept.getOutputStream());
            if (servletMap.get(request.getUrl()) == null) {
                response.outputHtml(request.getUrl());
            } else {
                HttpServlet httpServlet = servletMap.get(request.getUrl());
                httpServlet.service(request, response);
            }

            accept.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
